#include <bits/stdc++.h>

using namespace std;
#define id(a) a[0]
#define cpu(a) a[1]
#define mem(a) a[2]
#define supportN(a) a[3]
#define supportA(a) a[4]


struct comp1 {
    bool operator() (vector<int>& a, vector<int>& b) {
        if (cpu(a) > cpu(b)) {
            return true;
        } else if (cpu(a) == cpu(b)) {
            if (mem(a) > mem(b)) {
                return true;
            } else {
                return false;
            }
        } else {
            false;
        }
    }
};

struct comp2 {
    bool operator() (vector<int>& a, vector<int>& b) {
        if (mem(a) > mem(b)) {
            return true;
        } else if (mem(a) == mem(b) && (cpu(a) > cpu(b))) {
            return true;
        } else {
            false;
        }
    }
};

class comp {
    public:
        comp() : _policy(1) {}
        comp(int policy) : _policy(policy) {}
        bool operator() (vector<int>& a, vector<int>& b) {
            cout << "current policy " << _policy;
            if (_policy == 1) {
                if (cpu(a) > cpu(b)) {
                    return true;
                } else if (cpu(a) == cpu(b) && mem(a) > mem(b)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                if (mem(a) > mem(b)) {
                    return true;
                } else if (mem(a) == mem(b) && (cpu(a) > cpu(b))) {
                    return true;
                } else {
                    false;
                }
            }
        }
    private:
        int _policy;
};



int main() {
    
    int total_machines, tmp;
    cin >> total_machines;
    vector<vector<int> > machine_resource(total_machines);
    for (int i = 0; i < total_machines; ++i) {
        for (int j = 0; j < 5; ++j) {
            cin >> tmp;
            machine_resource[i].push_back(tmp);
            cin.ignore();
        }
    }

    int req_num, policy;
    vector<int> req_resource(5);
    cin >> req_num;
    cin >> policy;
    for (int i=1; i < 5; ++i) {
        cin >> tmp;
        cout << tmp;
        req_resource[i] = tmp;
    }

    //use priority queue
    priority_queue<vector<int>, vector<vector<int> >, comp > allocate_queue;

    if (policy == 1) {
        // policy 1
        // priority_queue<vector<int>, , comp1 > allocate_queue;
        // allocate_queue = new priority_queue<vector<int>, vector<vector<int> >, comp1 >();
        allocate_queue = priority_queue<vector<int>, vector<vector<int> >, comp >(comp(1));
    } else {
        // priority_queue<vector<int>, vector<vector<int> >, comp2 > allocate_queue;
        allocate_queue = priority_queue<vector<int>, vector<vector<int> >, comp >(comp(2));
    }

    for (auto machine : machine_resource) {
        if (supportA(req_resource) == 2 && (cpu(machine) == 9) && (mem(machine) >= mem(req_resource)) && 
            (supportN(machine) == supportN(req_resource))) {
                allocate_queue.push(machine);
        } else if (supportA(req_resource) == 2 && (cpu(machine) >= cpu(req_resource)) && (mem(machine) >= mem(req_resource)) && 
            (supportN(machine) == supportN(req_resource))) {
                allocate_queue.push(machine);
        } else if (cpu(machine) == 9 && (mem(machine) >= mem(req_resource)) && (supportN(machine) == supportN(req_resource)) && 
            (supportA(machine) == supportA(req_resource)) )  {
                allocate_queue.push(machine);
        } else if (cpu(machine) == cpu(req_resource) && (mem(machine) >= mem(req_resource)) && 
        (supportA(machine) == supportA(machine)) && (supportN(machine) == supportN(req_resource))) {
            allocate_queue.push(machine);
        } else {
            continue;
        }
    } 

    string output;
    output += to_string(min((int)allocate_queue.size(), req_num));
    if (allocate_queue.empty()) {
        cout << output;
        return 0;
    }
    for (int i = 0; i < req_num && (!allocate_queue.empty()); ++i) {
        output.push_back(' ');
        output += to_string(id(allocate_queue.top()));
        allocate_queue.pop();
    }
    cout << output;

    return 0;
}